﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IntroduceCompany.Singleton;
using IntroduceCompany.DTO;
using System.Data.Linq;
using System.ComponentModel.DataAnnotations;
using IntroduceCompany.Util;

namespace IntroduceCompany.Models
{

    public class CompanyInformationView_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(IntroduceCompany.Models.CompanyInformationView_Validation))]

        [Required(ErrorMessage = "Tên công ty phải có.")]
        public String CompanyName { get; set; }

        [RegularExpression(ConstantClass.testEmailRegular_1, ErrorMessage = "Không đúng định dạng email.")]
        public string Email { get; set; }

        [RegularExpression(ConstantClass.testPhoneRegular_1, ErrorMessage = "Không đúng định dạng điện thoại.\n Vd: ## or +##.## or #.##.## or +##.## or ## ##.## or (##) ##.##")]
        public string Phone { get; set; }

    }



    public class CompanyInformationModels
    {


        /// <summary>
        /// Get One record of Company Information View filter by languageID
        /// </summary>
        /// <param name="languageID"></param>
        /// <returns></returns>
        internal CompanyInformationView GetCompanyInformationView(String languageID)
        {
            try
            {
                return DataSingleton.GetInstance().CompanyInformationViews.First(com => com.LanguageID == languageID);
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// Get Information of Company table based on comID
        /// </summary>
        /// <param name="comID"></param>
        /// <returns></returns>
        internal CompanyInformation GetCompanyInformationByComID(int comID)
        {
            return DataSingleton.GetInstance().CompanyInformations.SingleOrDefault(com => com.ComID == comID);
        }


        /// <summary>
        /// Update new information for company
        /// </summary>
        /// <param name="companyView"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        internal bool EditCompanyInformation(CompanyInformationView companyView, String username)
        {
            try
            {
                CompanyInformation tempCom = DataSingleton.GetInstance().CompanyInformations.SingleOrDefault(com => com.ComID == companyView.ComID);
                tempCom.Phone = companyView.Phone;
                tempCom.Email = companyView.Email;
                tempCom.Fax = companyView.Fax;
                tempCom.UserName = username;

                CompanyInformation_L temCom_L = DataSingleton.GetInstance().CompanyInformation_Ls.SingleOrDefault(comL => comL.ComID == companyView.ComID
                    && comL.LanguageID == companyView.LanguageID);
                temCom_L.CompanyName = companyView.CompanyName;
                temCom_L.Address = companyView.Address;

                DataSingleton.GetInstance().SubmitChanges();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }




        /// <summary>
        /// Insert another language for Company Information
        /// </summary>
        /// <param name="company"></param>
        internal bool InsertCompanyInformationLanguage(CompanyInformationView company)
        {
            try
            {
                CompanyInformation_L tempComL = new CompanyInformation_L();
                tempComL.ComID = company.ComID;
                tempComL.LanguageID = company.LanguageID;
                tempComL.CompanyName = company.CompanyName;
                tempComL.Address = company.Address;

                DataSingleton.GetInstance().CompanyInformation_Ls.InsertOnSubmit(tempComL);
                DataSingleton.GetInstance().CompanyInformation_Ls.Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }



        /// <summary>
        /// Check company information language that is existed or not. True is existed and otherwise
        /// </summary>
        /// <param name="p"></param>
        /// <param name="p_2"></param>
        /// <returns></returns>
        internal bool CheckCompanyInformationLanguageIsExisted(int comID, String languageID)
        {
            return DataSingleton.GetInstance().CompanyInformation_Ls.Any(c => c.ComID == comID && c.LanguageID == languageID);
        }
    }
}